<script module>
	import { tv } from 'tailwind-variants';
	export const sheetVariants = tv({
		base: 'bg-background data-[state=open]:animate-in data-[state=closed]:animate-out fixed z-50 flex flex-col gap-4 shadow-lg transition ease-in-out data-[state=closed]:duration-300 data-[state=open]:duration-500',
		variants: {
			side: {
				top: 'data-[state=closed]:slide-out-to-top data-[state=open]:slide-in-from-top inset-x-0 top-0 h-auto border-b',
				bottom:
					'data-[state=closed]:slide-out-to-bottom data-[state=open]:slide-in-from-bottom inset-x-0 bottom-0 h-auto border-t',
				left: 'data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left inset-y-0 start-0 h-full w-3/4 border-e sm:max-w-sm',
				right:
					'data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right inset-y-0 end-0 h-full w-3/4 border-s sm:max-w-sm'
			}
		},
		defaultVariants: {
			side: 'right'
		}
	});
</script>

<script>
	import { Dialog as SheetPrimitive } from 'bits-ui';
	import SheetOverlay from './sheet-overlay.svelte';
	import { cn } from '$lib/utils.js';

	/**
	 * @type {{
	 *   ref?: HTMLElement | null,
	 *   class?: string,
	 *   side?: 'top' | 'bottom' | 'left' | 'right',
	 *   portalProps?: Record<string, any>,
	 *   children?: import('svelte').Snippet,
	 *   [key: string]: any
	 * }}
	 */
	let {
		ref = $bindable(null),
		class: className,
		side = 'right',
		portalProps = {},
		children,
		...restProps
	} = $props();
</script>

<SheetPrimitive.Portal {...portalProps}>
	<SheetOverlay />
	<SheetPrimitive.Content
		bind:ref
		data-slot="sheet-content"
		class={cn(sheetVariants({ side }), className)}
		{...restProps}
	>
		{@render children?.()}
	</SheetPrimitive.Content>
</SheetPrimitive.Portal>
